<?php
/**
 * 注释不留名,代码随便用
 * Date: 2019/5/27
 * Time: 15:52
 */

namespace app\common\library\basicadmin\logic;

use app\common\controller\BasicAdmin;
use app\common\library\basicadmin\Logic;
use think\Model;
use traits\controller\Jump;

/**
 * 导出excel
 * Class Export
 * @package app\common\library\basicadmin\logic
 */
class Export extends Logic
{
    protected $table_field;
    protected $list;
    protected $title;
    protected $sheet; //excel表当前table
    protected $php_excel;
    use Jump;
    /**
     * Export constructor.
     * @param $list array 数据列表
     * @param $table_field
        @格式:$table_field = [
            ['A','index' , '序号'],
            ['B','archive_num' ,'档案号'],
            ['C','work_num' , '工号']
        ];
     * @param $title
     */
    public function __construct($list, $table_field, $title)
    {
        parent::__construct();
        $this->list = $list;
        $this->table_field = $table_field;
        $this->title = $title;
    }


    public function init(BasicAdmin $controller)
    {
        if(empty($this->list)){
             $this->error('没有数据啦！');
        }
        $this->php_excel = new \PHPExcel();
        $this->sheet = $this->php_excel->getActiveSheet();
        $this->php_excel->getProperties()->setTitle($this->title);
        $this->sheet->setTitle($this->title);
        $this->writeData();
        $this->output();

    }

    /**
     * 输出excel
     * @throws \PHPExcel_Writer_Exception
     */
    protected function output(){
        $objWriter = new \PHPExcel_Writer_Excel5($this->php_excel);
        ob_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="'.$this->title.'.xls"');
        header("Content-Transfer-Encoding:binary");
        //Excel5 类 将所有在switch内使用continue 全部改成 break  ; 不然在php7.3中会错误
        $objWriter->save('php://output');
        exit;
    }

    /**
     * 写入数据
     * @param $list
     */
    protected function writeData(){
        //起始行数，从第1行开始写数据
        $line_num = 1;
        foreach($this->table_field as $v){
            list($index, $field, $title) = $v;
            if($field != 'index') {
                $this->sheet->getColumnDimension($index)->setWidth(20);
            }
            $this->sheet->setCellValue($index . $line_num, $title);
            $this->sheet->getStyle($index.$line_num)->getFont()->setBold(true);
        }
        $line_num ++;
        foreach($this->list as $k => $vo){
            foreach($this->table_field  as $v){
                list($index, $field, $title) = $v;
                //写入序号
                if($field == 'index'){
                    $val = $k + 1;
                } else if ($field instanceof \Closure){
                   $val =  call_user_func($field, $vo);
                }else if(strpos($field, '.')){
                    list($prefix, $field) = explode('.', $field);
                    $val = $vo[$prefix][$field];
                }
                else{
                    $val = $vo[$field];
                }
                $arr[] = [
                    $index . $line_num =>  $val
                ];
//                $this->sheet->getStyle($index . $line_num)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $this->sheet->getStyle($index.$line_num)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $this->sheet->setCellValueExplicit($index . $line_num, $val,\PHPExcel_Cell_DataType::TYPE_STRING);
                foreach(['top', 'left', 'bottom', 'right'] as $b){
                    $fun = 'get'.$b;
                    $this->sheet->getStyle($index . $line_num)->getBorders()->$fun()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                }
            }
            $line_num++;
        }
    }


}